package com.example.administrator.szb.activity.myyewu;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BaseActivity;
import com.example.administrator.szb.adapter.BaseAdapter;
import com.example.administrator.szb.bean.ResultBean;
import com.example.administrator.szb.bean.YWBQGLBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.DialogUtil;
import com.example.administrator.szb.util.GetJsonDataUtil;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by D N on 2017/12/4.
 */

public class YWXQActivity_save_bgzr extends BaseActivity {
    public YWBQGLBean ywbqglBean;
    YWBQGLBean.DataBean dataBean;
    HashMap<Integer, YWBQGLBean.DataBean> data_map = new HashMap<>();
    BaseAdapter<YWBQGLBean.DataBean.NextBeanX> adapter_nextX;
    ArrayList<YWBQGLBean.DataBean.NextBeanX> mdata = new ArrayList<YWBQGLBean.DataBean.NextBeanX>(){
        @Override
        public String toString() {
            String info_id = "";
            for (int i = 0; i < mdata.size(); i++) {
                if (mdata.get(i).getGenre() == 1){
                    info_id = info_id + mdata.get(i).getId() +",";
                }
            }
            return info_id;
        }
    };

    @Bind(R.id.ywxq_no_image_fh)
    ImageView ywxqNoImageFh;
    @Bind(R.id.ywxq_no_text_title)
    TextView ywxqNoTextTitle;
    @Bind(R.id.ywxq_no_recyclerview)
    RecyclerView ywxqNoRecyclerview;
    @Bind(R.id.ywxq_no_edit_xgms)
    EditText ywxqNoEditXgms;
    @Bind(R.id.ywxq_no_text_zs)
    TextView ywxqNoTextZs;
    @Bind(R.id.ywxq_no_xgms)
    LinearLayout ywxqNoXgms;
    @Bind(R.id.ywxq_no_button_bc)
    Button ywxqNoButtonBc;
    private int id=0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wyxq_activity);
        ButterKnife.bind(this);
        SampleApplicationLike.getActivitiesInstance().add(this);
        editListenter(ywxqNoEditXgms,ywxqNoTextZs);
        id = getIntent().getIntExtra("id", -1);
        requestData();
    }

    private void requestData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("users_id", SampleApplicationLike.getUserloginInstance().getId() + "");
        String json = GetJsonDataUtil.simpleMapToJsonStr(map);
        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, YWBQGLBean.class, URLAddress.URLBASE + URLAddress.URL_YWBQGL, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                ywbqglBean = (YWBQGLBean) response;
                if (ywbqglBean.getResult() == 1) {
                    for (int i = 0; i < ywbqglBean.getData().size(); i++) {
                        data_map.put(ywbqglBean.getData().get(i).getId(), ywbqglBean.getData().get(i));
                    }
                    mdata.addAll(data_map.get(id).getNext());
                    ywxqNoEditXgms.setText(data_map.get(id).getDescribe());
                    initRecyclerView();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //startActivity(new Intent(YWXQActivity_save.this,YWGLNOActivity_new.class));
            }
        });
    }

    private void initRecyclerView() {
        adapter_nextX = new BaseAdapter<YWBQGLBean.DataBean.NextBeanX>(this,mdata) {
            @Override
            public void convert(final int position, BaseViewHolder holder, YWBQGLBean.DataBean.NextBeanX nextBeanX) {
                ((TextView)holder.getView(R.id.ywxq_no_popuwindow_text_item)).setText(nextBeanX.getTitle());

                ((CheckBox)holder.getView(R.id.ywgl_no_popuwindow_checkbox_szgl)).setEnabled(false);
                ((CheckBox)holder.getView(R.id.ywgl_no_popuwindow_checkbox_szgl)).setClickable(false);

                if (nextBeanX.getGenre() == 1){
                    ((CheckBox)holder.getView(R.id.ywgl_no_popuwindow_checkbox_szgl)).setChecked(true);
                }else{
                    ((CheckBox)holder.getView(R.id.ywgl_no_popuwindow_checkbox_szgl)).setChecked(false);
                }
                ((CheckBox)holder.getView(R.id.ywgl_no_popuwindow_checkbox_szgl)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        if (b){
                            mdata.get(position).setGenre(1);
                        }else{
                            mdata.get(position).setGenre(0);
                        }
                    }
                });
            }

            @Override
            public int getLayoutId(int viewType) {
                return  R.layout.view_ywxq_popuwindow_item;
            }
        };

        adapter_nextX.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                int flag = mdata.get(position).getGenre();

                if (flag == 1){
                    ((CheckBox)view.findViewById(R.id.ywgl_no_popuwindow_checkbox_szgl)).setChecked(false);
                }else if (flag == 0){
                    ((CheckBox)view.findViewById(R.id.ywgl_no_popuwindow_checkbox_szgl)).setChecked(true);
                }
            }
        });

        ywxqNoRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        ywxqNoRecyclerview.setAdapter(adapter_nextX);
    }

    @OnClick({R.id.ywxq_no_image_fh, R.id.ywxq_no_button_bc})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ywxq_no_image_fh:
                finish();
                break;
            case R.id.ywxq_no_button_bc:
                boolean flag = checkInfo();
                if (flag){
                    doSubmitRequest();
                    /*if (!TextUtils.isEmpty(ywxqNoEditXgms.getText().toString().trim())){

                    }else{
                        DialogUtil.showToast(YWXQActivity_save_bgzr.this,"请输入业务描述");
                    }*/

                }else{
                    DialogUtil.showToast(YWXQActivity_save_bgzr.this,"请选择业务类型");
                }
                break;
        }
    }
    private void doSubmitRequest() {
        HashMap<String,String > map = new HashMap<>();
        map.put("code",HttpUtil.getParam());
        map.put("users_id",SampleApplicationLike.getUserloginInstance().getId()+"");
        map.put("business_id",mdata.toString()+id);
        map.put("describe",""+ywxqNoEditXgms.getText().toString().trim());

        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, ResultBean.class, URLAddress.URLBASE + URLAddress.URL_YWBQSC, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                ResultBean bean = (ResultBean) response;
                if (bean.getResult() == 1){
                    // YWGLNOActivity.isVisiable.put(""+title,1);//1,代表显示已经保存
                    YWGLNOActivity_new.businessBean_yw.getData().get(getIntent().getIntExtra("position",-1)).setIsChoose(1);
                    finish();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                DialogUtil.showToast(YWXQActivity_save_bgzr.this,"请勿输入表情等特殊性字符");
            }
        });
    }
    private boolean checkInfo() {
        for (int i = 0; i < mdata.size(); i++) {
            if (mdata.get(i).getGenre() == 1){
                return true;
            }
        }
        return false;
    }
}
